<?php
namespace Home\Controller;
use Think\Controller;

/**
 * api首页
 */
class IndexController extends HomeBasicController {

	public $Project = '';

	public function _initialize(){
		parent::_initialize();
		$this->Project = D('Project');

	}

	/**
	 * 显示当前登陆的项目
	 */
    public function index(){
    	$this->checkLogin();
    	$mid = session('M_ID');
    	$mtype = session('M_TYPE');
    	if($mtype=='2'){
    		$where['own_m_id'] = $mid;
            $where['is_show'] = 1;
            $where['status'] = 1;
            $where['_logic'] = 'OR';
    		$project_list = $this->Project->selectProject($where,'ctime desc',300);
    	}else{
    		$project_list = $this->Project->selectProject('','ctime desc',300);
    	}
        $this->assign('active',1);
    	$this->assign('project_list',$project_list['list']);
		$this->display();
	}

    /**
     * 导出数据
     */
    public function exportExcel(){
        header("Content-type: text/html; charset=utf-8");
        //实例化对象
        $util_obj = D('Util','Service');
        $Project = D('Project');
        $Module = D('Module');
        $Function = D('Function');
        $where_pro['p_id'] = $_POST['p_id'];
        $project_info = $Project->where($where_pro)->find();

        //获取项目下模块列表
        $where_mod['p_id'] = $_POST['p_id'];
        $model_list = $Module->where($where_mod)->select();
        $bbb = array();
        foreach($model_list as $k=>$v){
            //获取模块下方法列表
            $where_fun['mo_id'] = $v['mo_id'];
            $model_list[$k]['function_list'] = $Function->where($where_fun)->select();
            foreach($model_list[$k]['function_list'] as $kk=>$vv){
                //整合方法数据
                //获取方法所属项目名称
                $model_list[$k]['function_list'][$kk]['p_name'] = $project_info['p_name'];
                //获取方法所属模块名称
                $model_list[$k]['function_list'][$kk]['mo_name'] = $Module->where(array('mo_id'=>$vv['mo_id']))->getField('mo_name');
                //拆分整理方法参数
                $model_list[$k]['function_list'][$kk]['f_pa_arr'] = unserialize($vv['f_pa']);
                $model_list[$k]['function_list'][$kk]['f_name'] = $vv['f_name'];
                if(is_array($model_list[$k]['function_list'][$kk]['f_pa_arr'])){
                    foreach($model_list[$k]['function_list'][$kk]['f_pa_arr'] as $kkk=>$vvv){
                        //获取参数所属项目名称
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_name'] = $project_info['p_name'];
                        //获取参数所属模块名称
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['mo_name'] = $Module->where(array('mo_id'=>$vv['mo_id']))->getField('mo_name');
                        //获取参数所属方法名称
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_zh_name'] = $vv['f_zh_name'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_url'] = $vv['f_url'];

                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_type'] = $vvv['p_type'] ? "图片" : "文本";
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['pa_name'] = $vvv['p_name'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_desc'] = $vvv['p_desc'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['p_is_null'] = $vvv['p_is_null'] ? "是" : "否";

                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_type'] = $vv['f_type'] ? "POST": "GET";
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_success'] = $vv['f_success'];
                        $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]['f_error'] = $vv['f_error'];


                        $bbb[] = $model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk];

                        //dump($model_list[$k]['function_list'][$kk]['f_pa_arr'][$kkk]);

                    }
                }else{
                    //获取参数所属项目名称
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_name'] = $project_info['p_name'];
                    //获取参数所属模块名称
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['mo_name'] = $Module->where(array('mo_id'=>$vv['mo_id']))->getField('mo_name');
                    //获取参数所属方法名称
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_zh_name'] = $vv['f_zh_name'];
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_url'] = $vv['f_url'];

                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_type'] = "无参数";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['pa_name'] = "无参数";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_desc'] = "无参数";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['p_is_null'] = "无参数";

                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_type'] = $vv['f_type'] ? "POST": "GET";
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_success'] = $vv['f_success'];
                    $model_list[$k]['function_list'][$kk]['f_pa_arr'][0]['f_error'] = $vv['f_error'];
                    $bbb[] = $model_list[$k]['function_list'][$kk]['f_pa_arr'][0];
                }
            }
        }
        //字段数据
        $fields_data = I('post.fields_data');
        //执行导出函数
        $util_obj->exportExcel($fields_data,$bbb,$project_info['p_name'].'API');
    }
	
}
